package com.jingan.nursinghome.regulatory.view

import android.app.Activity
import android.content.Context
import android.graphics.Bitmap
import android.graphics.Color
import android.webkit.WebView
import com.jingan.nursinghome.regulatory.view.H5
import android.webkit.WebViewClient
import android.webkit.WebSettings
import android.webkit.WebChromeClient
import android.graphics.Color.parseColor
import android.view.View
import cn.android.support.v7.lib.sin.crown.kotlin.common.Progressbar


class H5 : WebView {
    val progressbar: Progressbar by lazy { Progressbar(context as Activity, true, false) }

    constructor(context: Context) : super(context) {
        setBackground(null)
        //setBackgroundColor(Color.parseColor("#00000000"))
        setBackgroundColor(Color.WHITE)
        //setLayerType(View.LAYER_TYPE_SOFTWARE, null);//关闭硬件加速(默认是开启的)，确保背景透明有效。不关闭也没事，一般都有效。如果关闭了，视频会无法播放。所以建议开启。

        setWebChromeClient(WebChromeClient()) //（一定要加）
        //设置字符集编码
        getSettings().setDefaultTextEncodingName("UTF-8")
        //开启JavaScript支持
        getSettings().setJavaScriptEnabled(true)
        //自适应手机屏幕，完整的显示页面
        getSettings().setUseWideViewPort(true)
        getSettings().setLoadWithOverviewMode(true)
        getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL)
        getSettings().setDomStorageEnabled(true)
        setInitialScale(1)
        getSettings().setLoadsImagesAutomatically(true)
        loadData("<meta name=\"viewport\" content=\"width=device-width\">\n" + "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no\">", "text/html", "UTF-8")
        //不支持缩放
        getSettings().setSupportZoom(false)
        //取消滚动条
        setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY)
        //清除缓存
        clearCache(true)
        //不从缓存中读取，仅从网络中读取
        getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE)
        setWebViewClient(object : WebViewClient() {
            override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
                // TODO Auto-generated method stub
                view.loadUrl(url)
                //继续在当前browser中响应
                return true
            }

            override fun onPageFinished(view: WebView?, url: String?) {
                super.onPageFinished(view, url)
                //页面加载完成
                progressbar.dismiss()
            }

            override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
                super.onPageStarted(view, url, favicon)
                //开始加载页面
                progressbar.isLocked(false).isDarkmode(true).show()
            }

        })
    }
}